//
//  GSDKServicesManager.m
//  GameSDK
//
//  Created by Johnny on 2017/7/28.
//  Copyright © 2017年 Johnny. All rights reserved.
//

#import "GSDKServicesManager.h"
#import "YYKit.h"
#import "GSDKServices.h"
#import "AFNetworking.h"
#import "GSDKCommonMethod.h"
@interface GSDKServicesManager ()

@end

@implementation GSDKServicesManager
- (void)dealloc {
    DEBUGMSG(@"GSDKServicesManager dealloc");
    [super dealloc];
}

- (void)clean {
    if ([self retainCount] == 2) {
        [self release];
    }
    [self release];
}

- (void)request:(GSDKModel *)model andCompletionBlock:(GSDKCompletionBlock)block {
    __block GSDKServicesManager * bSelf = self;
    [self request:model completionBlock:^(id responseObject) {
        block(responseObject);
        [bSelf clean];
    } failureBlock:^{
        [bSelf request:model andCompletionBlock:block];
    }];
}

- (void)iapRequest:(GSDKModel *)model andTime:(float)time andCompletionBlock:(GSDKCompletionBlock)block {
    __block GSDKServicesManager * bSelf = self;
    [self iapRequest:model completionBlock:^(id responseObject) {
        GSDKResIAPModel * iapModel = [GSDKResIAPModel modelWithJSON:responseObject];
        if ([iapModel.code isEqualToString:@"0"] || [iapModel.code isEqualToString:@"1000"]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (block) {
                    block(iapModel);
                }
                [bSelf clean];
            });
        } else {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                [bSelf iapRequest:model andTime:time andCompletionBlock:block];
            });
        }
    } failureBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [bSelf iapRequest:model andTime:time andCompletionBlock:block];
        });
    }];
}

- (void)oneTimeRequest:(GSDKModel *)model andCompletionBlock:(GSDKCompletionBlock)block {
    @weakify(self)
    [self request:model completionBlock:^(id responseObject) {
        @strongify(self)
        if (block) {
            block(responseObject);
        }
        [self clean];
    } failureBlock:^{
    }];
}

- (void)request:(GSDKModel *)model completionBlock:(GSDKCompletionBlock)completionBlock failureBlock:(void (^)())failureBlock {
    if ([self retainCount] <= 1) { [self retain]; }
    DEBUGMSG(@"[model jsonModel] = %@", [model jsonModel]);
    [[[GSDKServices shared] manager] POST:DomainUrl parameters:[model jsonModel] progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject != nil) {
            DEBUGMSG(@"responseObject = %@", responseObject);
            completionBlock(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failureBlock();
    }];
}

- (void)iapRequest:(GSDKModel *)model completionBlock:(GSDKCompletionBlock)completionBlock failureBlock:(void (^)())failureBlock {
    if ([self retainCount] <= 1) { [self retain]; }
    DEBUGMSG(@"[model jsonModel] = %@", [model jsonModel]);
    [[[GSDKServices shared] manager] POST:IAP_DomainUrl parameters:[model jsonModel] progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject != nil) {
            completionBlock(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failureBlock();
    }];
}

- (void)urlRequest:(NSString *)urlString completionBlock:(GSDKCompletionBlock)completionBlock failureBlock:(void (^)())failureBlock {
    if ([self retainCount] <= 1) { [self retain]; }
    [[[GSDKServices shared] manager] GET:urlString parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject != nil) {
            completionBlock(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failureBlock();
    }];
}


@end

